package ttOrder.controller;

import static org.hamcrest.CoreMatchers.nullValue;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import ttOrder.dao.OrderMapper;
import ttOrder.model.Order;
import ttOrder.service.IMycartService;
import ttOrder.service.IOrderService;
import ttOrder.vo.CommonMessageResultVO;
import ttOrder.vo.OrderDetallVO;
import ttOrder.vo.OrderVO;

@Controller
public class OrderController {

	@Autowired
	public IOrderService orderService;
	@Autowired
	public OrderMapper orderMapper;
	//显示全部订单
	@RequestMapping("/showAllOrder")
	public ModelAndView allorder(Model model){
		model.addAttribute("order",orderService.findAllOrder());
		return new ModelAndView("AdminOrder/showAllOrder");
	}
	
	//删除订单
	@RequestMapping(value="/deleteOrder",method=RequestMethod.POST)
	@ResponseBody
	public CommonMessageResultVO delorder(int orderId,int orderstatus){
		CommonMessageResultVO resultVO = new CommonMessageResultVO();
		resultVO.setSuccess(false);
//		System.out.println("ggg");
//		System.out.println(orderId+"---"+orderstatus);
		if(orderstatus==0){
		orderService.deleteOrder(orderId);
		orderService.deleteDetall(orderId);
		resultVO.setSuccess(true);
		}else{
			System.out.println("delete fail");;
		}
		return resultVO;
	}
	//根据订单号查询详细订单之页面跳转方式
//	@RequestMapping(value="/findByOrderId",method=RequestMethod.POST)
//	@ResponseBody
//	public List<OrderDetallVO> findByOrderId(Integer orderId){
//		List<OrderDetallVO> list=orderService.findOrderById(orderId);
//		
//		return list;
//		
//	}
	
//	@RequestMapping(value="/findByOrderId/{orderId}", produces = "application/json; charset=utf-8")
//	public String findByOrderId(@PathVariable("orderId") int orderId, Model model){
//		System.out.println("lai");
//		
//		List<OrderDetallVO> list=orderService.findDetallById(orderId);
//		model.addAttribute("orderdetall",list);
//		model.addAttribute("order",orderService.findOrderById(orderId));
//		return "AdminOrder/showfindorder";
//		
//	}
	//根据订单号查询详细订单之模态框方式
	@RequestMapping(value="/findByOrderId")
	@ResponseBody
	public Map<String,Object> findByOrderId(int orderId){
		Map<String,Object> resultMap=new HashMap<String,Object>();
//		List<OrderDetallVO> list=orderService.findDetallById(orderId);
//		model.addAttribute("orderdetall",list);
//		model.addAttribute("order",orderService.findOrderById(orderId));
		resultMap.put("orderdetall", orderService.findDetallById(orderId));
		resultMap.put("order",orderService.findOrderById(orderId));
		return resultMap;
		
		
	}
	
	//修改订单状态
		@RequestMapping(value="/editOrder",method = RequestMethod.POST)
		@ResponseBody
		public CommonMessageResultVO editOrder(int orderId){
			//System.out.println("qiu");
			CommonMessageResultVO resultVO = new CommonMessageResultVO();
			resultVO.setSuccess(false);
			try {
				OrderVO orderVO=new OrderVO();
				orderVO.setOrderId(orderId);
				orderService.updateOrder(orderVO);;
				resultVO.setSuccess(true);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return resultVO;
		}
		
		
		//根据订单号查询详细订单之模态框方式(前台显示)
		@RequestMapping(value="/findByOrdermaxId")
		@ResponseBody
		public Map<String,Object> findByOrdermaxId(){
			//System.out.println("nihao"+orderMapper.getOrderId());
			Map<String,Object> resultMap=new HashMap<String,Object>();
			Integer orderId=orderMapper.getOrderId();
			
//			List<OrderDetallVO> list=orderService.findDetallById(orderId);
//			model.addAttribute("orderdetall",list);
//			model.addAttribute("order",orderService.findOrderById(orderId));
			resultMap.put("orderdetall", orderService.findDetallById(orderId));
			resultMap.put("order",orderService.findOrderById(orderId));
			return resultMap;
			
			
		}
}
